package com.he.architecture.controller;

import com.he.architecture.common.controller.LogicController;
import com.he.architecture.dao.LineItemDao;
import com.he.architecture.dto.EntityDtos.LineItemResponse;
import com.he.architecture.dto.ResponseResult;
import com.he.architecture.entity.LineItem;
import com.he.architecture.service.LineItemService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/lineItem")
public class LineItemController extends LogicController<LineItemService, LineItemDao, LineItem,Long> {
    @Resource
    ModelMapper modelMapper;
    public LineItemController(@Autowired LineItemService service){
        super(service);
    }

    @GetMapping("/getLineItemsByCustomerOrderId")
    public ResponseResult getLineItemsByCustomerOrderId(@RequestParam("cus_order_id") Long cus_order_id) {
        List<LineItem> lineItems = getService().getLineItemsByCustomerOrderId(cus_order_id);

        Map<String,Object> map=new HashMap<>();
        map.put("lineItems",lineItems.stream().map(lineItem -> modelMapper.map(lineItem, LineItemResponse.class)).collect(Collectors.toSet()));
        return new ResponseResult(200,map);
    }
}
